package com.lancoo.answer.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.ViewParent;
import android.webkit.WebView;

import androidx.annotation.Nullable;

/**
 * @Describe 可交互的webView
 * 点击事件的高度处理
 */

public class KWebView extends WebView {
    public OnScrollChangeListener listener;
    private static final String TAG = "KWebView";
    private boolean isDisableInterceptTouch = false;

    public void setDisableInterceptTouch(boolean disableInterceptTouch) {
        isDisableInterceptTouch = disableInterceptTouch;
    }

    public KWebView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public KWebView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public KWebView(Context context) {
        super(context);
    }

    @Override
    protected void onScrollChanged(int l, int t, int oldL, int oldT) {
        super.onScrollChanged(l, t, oldL, oldT);
        // webView的高度
        float webContent = getContentHeight() * getScale();
        // 当前webView的高度
        float webNow = getHeight() + getScrollY();
        if (Math.abs(webContent - webNow) < 1) {
            // 已经处于底端
            if (listener != null)
                listener.onPageEnd(l, t, oldL, oldT);
        } else if (getScrollY() == 0) {
            if (listener != null)
                listener.onPageTop(l, t, oldL, oldT);
        } else {
            if (listener != null)
                listener.onScrollChanged(l, t, oldL, oldT);
        }
    }

    @Override
    public void setOnLongClickListener(@Nullable OnLongClickListener l) {
        super.setOnLongClickListener(l);
    }

    public void setOnScrollChangeListener(OnScrollChangeListener listener) {
        this.listener = listener;
    }

    public interface OnScrollChangeListener {
        void onPageEnd(int l, int t, int oldl, int oldt);

        void onPageTop(int l, int t, int oldl, int oldt);

        void onScrollChanged(int l, int t, int oldl, int oldt);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {
        Log.e(TAG, "onInterceptTouchEvent:" + event.getAction());
        return super.onInterceptTouchEvent(event);
    }


    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (isDisableInterceptTouch) {
            return super.onTouchEvent(event);
        }
        int x = (int) event.getRawX();
        int y = (int) event.getRawY();
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                break;
            case MotionEvent.ACTION_MOVE:
                if (getScrollX() == 0 && Math.abs(getScrollY()) < 5) {
                    super.requestDisallowInterceptTouchEvent(false);
                } else {
                    super.requestDisallowInterceptTouchEvent(true);
                }
                break;
            case MotionEvent.ACTION_CANCEL:
            case MotionEvent.ACTION_UP:
                super.requestDisallowInterceptTouchEvent(false);
            default:
                break;
        }
        return super.onTouchEvent(event);
    }

    @Override
    protected void onOverScrolled(int scrollX, int scrollY, boolean clampedX, boolean clampedY) {
        if (clampedX) {//水平判断
            super.requestDisallowInterceptTouchEvent(false);
        }
        super.onOverScrolled(scrollX, scrollY, clampedX, clampedY);
    }

    private ViewParent findParentIfNeed(ViewParent view) {
        if (view instanceof ViewPager2Container) {
            return view;
        } else {
            return findParentIfNeed(view.getParent());
        }
    }

}